#include "TestProj.h"

TestProj::TestProj(string results , bool DMRW , bool Klien , bool Shasha_Zhang)
{
	string timesFileName;
	path = results;
	path.erase(path.find_last_of("\\")+1);
	timesFileName = path+"times.txt";
	result.open(results.c_str());
	rtime.open(timesFileName.c_str());
	ChooseAlgorithm[0] = DMRW;
	ChooseAlgorithm[1] = Klien;
	ChooseAlgorithm[2] = Shasha_Zhang;
}

void TestProj::TestProjCreate(int rangeLimit ,Distance* distance)
{
	time_t start,end;
	Tree* F;
	Tree* G;
	string filename(path+"test");
	RTG generator(rangeLimit);
	//create a head start for the headline because of "i) "
	result<<"   ";
	rtime<<"   ";
	for(int j=0; j<3; j++)
	{
		if(ChooseAlgorithm[j] && j==0 )
		{
			result<<"DMRW|";
			rtime<<"DMRW|";
		}
		if(ChooseAlgorithm[j] && j==1 )
		{
			result<<"Klien|";
			rtime<<"Klien|";
		}
		if(ChooseAlgorithm[j] && j==2 )
		{
			result<<"Shasha & Zhang";
			rtime<<"Shasha & Zhang";
		}
	}
	result<<endl;
	rtime<<endl;
	result<<"--------------------------------------------------------------------------------"<<endl;
	rtime<<"--------------------------------------------------------------------------------"<<endl;
	for(int i=0; i<rangeLimit ; i++)
	{
		string cfilenameF; //current filename for tree F
		string cfilenameG; //current filename for tree G
		cfilenameG = cfilenameF = filename;
		if( i<10 )
			cfilenameG = cfilenameF += char('0' + i);
		else
		{
			cfilenameG = cfilenameF += char('0' + i/10);
			cfilenameG = cfilenameF += char('0' + i%10);
		}
		generator.Clear();
		generator.Generate();
		F = generator.getRoot();
		cfilenameF += "F.txt";
		F->Save(cfilenameF);
		generator.Clear();
		generator.Generate();
		G = generator.getRoot();
		cfilenameG += "G.txt";
		G->Save(cfilenameG);
		result<<i<<") ";
		rtime<<i<<") ";
		for(int j=0; j<3; j++)
		{
			if(ChooseAlgorithm[j])//check if the algorithm is included in the test
			{
				time(&start);
				TED test(F, G, distance, (Algorithm)i);
				time(&end);
				result<<test.GetTED()<<" ";
				rtime<<difftime(end,start)<<" ";
			}
		}
		result<<endl;
		rtime<<endl;
		//cout<<i<<endl;
		F->~Tree();
		G->~Tree();
	}
}

void TestProj::TestProjLoad(int amount ,Distance *distance)
{
	time_t start,end;
	Tree* F;
	Tree* G;
	string filename(path+"test");
	//create a head start for the headline because of "i) "
	result<<"   ";
	rtime<<"   ";
	for(int j=0; j<3; j++)
	{
		if(ChooseAlgorithm[j] && j==0 )
		{
			result<<"DMRW|";
			rtime<<"DMRW|";
		}
		if(ChooseAlgorithm[j] && j==1 )
		{
			result<<"Klien|";
			rtime<<"Klien|";
		}
		if(ChooseAlgorithm[j] && j==2 )
		{
			result<<"Shasha & Zhang";
			rtime<<"Shasha & Zhang";
		}
	}
	result<<endl;
	rtime<<endl;
	result<<"--------------------------------------------------------------------------------"<<endl;
	rtime<<"--------------------------------------------------------------------------------"<<endl;
	for(int i=0; i<amount ; i++)
	{
		string cfilenameF; //current filename for tree F
		string cfilenameG; //current filename for tree G
		cfilenameG = cfilenameF = filename;
		if( i<10 )
			cfilenameG = cfilenameF += char('0' + i);
		else
		{
			cfilenameG = cfilenameF += char('0' + i/10);
			cfilenameG = cfilenameF += char('0' + i%10);
		}
		cfilenameF += "F.txt";
		cfilenameG += "G.txt";
		F = Load(cfilenameF);
		G = Load(cfilenameG);
		result<<i<<") ";
		rtime<<i<<") ";
		for(int j=0; j<3; j++)
		{
			if(ChooseAlgorithm[j])//check if the algorithm is included in the test
			{
				time(&start);
				TED test(F, G, distance, (Algorithm)i);
				time(&end);
				result<< test.GetTED()<<" ";
				rtime<<difftime(end,start)<<" ";
			}
		}
		result<<endl;
		rtime<<endl;
	//	cout<<i<<endl;
		F->~Tree();
		G->~Tree();
	}
}